package com.example.seatastoragesys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.seatastoragesys.domain.Storage;
import com.example.seatastoragesys.service.StorageService;
import com.example.seatastoragesys.mapper.StorageMapper;
import io.seata.spring.annotation.GlobalLock;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage>
        implements StorageService {

    @Resource
    private StorageMapper storageMapper;


    @Override
    public void deduct(Long productId, int count) {
        Storage storage =
                storageMapper.selectOne(new LambdaQueryWrapper<Storage>()
                        .eq(Storage::getProductId, productId));
        if (storage==null){
            throw new RuntimeException("productId for storage not find exception");
        }
        storage.setUsed(storage.getUsed()+count);
        storage.setResidue(storage.getResidue() - count);
        storageMapper.updateById(storage);
    }

    //声明事务仅在单个本地RM中执行
    @GlobalLock
    @Override
    public Storage get(Long id) {
        return storageMapper.selectById(id);
    }

    @Override
    public void batchUpdate() {

    }

    @Override
    public void batchDelete() {

    }


}




